﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.CanBoLopService;
namespace eTMS.Controllers
{
    public class CanBoLopControllerBase : CommonController
    {
        public CanBoLopControllerBase()
            : base()
        {
            this.dao = new CanBoLopServiceClient();
        }
        ~CanBoLopControllerBase()
        {
            if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
        }
        private DataTable _Table;
        private CanBoLopEntity oCanBoLop;
        public CanBoLopEntity CanBoLopObject
        {
            get
            {
                return oCanBoLop;
            }
            set
            {
                oCanBoLop = value;
            }
        }
        protected CanBoLopServiceClient dao;
        public DataTable Table
        {
            get
            {
                return _Table;
            }
            set
            {
                _Table = value;
            }
        }
        public void AddIndexToTable()
        {
            this.AddIndexToTable(_Table);
        }
        public void ResetTableIndex()
        {
            this.ResetTableIndex(_Table);
        }
        public bool AddNew()
        {
            if (oCanBoLop == null) return false;
            bool result = true;
            try
            {
                oCanBoLop = dao.SaveRet(oCanBoLop);
            }
            catch
            {
                result = false;
            }
            if (result)
            {
                _ListCanBoLop.Add(oCanBoLop);
            }
            return result;
        }
        public bool Update()
        {
            if (oCanBoLop == null) return false;
            return dao.Update(oCanBoLop);
        }
        public bool Delete()
        {
            if (oCanBoLop == null) return false;
            bool retvalue = dao.Delete(oCanBoLop);
            if (retvalue)
            {
                _ListCanBoLop.Remove(oCanBoLop);
            }
            return retvalue;
        }
        public bool DeleteByID(Int32 CanBoLop_ID)
        {
            bool retvalue = dao.DeleteByID(CanBoLop_ID);
            if (retvalue)
            {
                CanBoLopEntity oDeleted = FindCanBoLop(CanBoLop_ID);
                if (oDeleted != null)
                {
                    _ListCanBoLop.Remove(oDeleted);
                }
            }
            return retvalue;
        }
        public void ResetObject()
        {
            this.oCanBoLop = new CanBoLopEntity();
        }
        //Copy data from data row in grid to object
        public CanBoLopEntity MapDataRowToObject(DataRow row)
        {
            CanBoLopEntity o = new CanBoLopEntity();
            if (!row.IsNull("ChucVuCanBoLop_ID"))
                o.ChucVuCanBoLop_ID = (Int32)(row["ChucVuCanBoLop_ID"]);
            if (!row.IsNull("LopSinhVien_ID"))
                o.LopSinhVien_ID = (Int32)(row["LopSinhVien_ID"]);
            if (!row.IsNull("Lop_ID"))
                o.Lop_ID = (Int32)(row["Lop_ID"]);
            if (!row.IsNull("SinhVien_ID"))
                o.SinhVien_ID = (Int32)(row["SinhVien_ID"]);
            if (!row.IsNull("CanBoLop_ID"))
                o.CanBoLop_ID = (Int32)(row["CanBoLop_ID"]);
            return o;
        }
        //Copy data from object to data row in grid    
        public void MapObjectToDataRow(CanBoLopEntity o, DataRow row)
        {
            row["ChucVuCanBoLop_ID"] = o.ChucVuCanBoLop_ID;
            row["LopSinhVien_ID"] = o.LopSinhVien_ID;
            row["CanBoLop_ID"] = o.CanBoLop_ID;
        }
        public void MapObjectToDataRow(DataRow row)
        {
            this.MapObjectToDataRow(this.CanBoLopObject, row);
        }
        public void LoadObject(Int32 CanBoLop_ID)
        {
            CanBoLopEntity oLoad = FindCanBoLop(CanBoLop_ID);
            if (oLoad == null)
            {
                oCanBoLop = this.LoadCanBoLop(CanBoLop_ID);
                bool bNotIsNull = true;
                bNotIsNull &= oCanBoLop.IsNullCanBoLop_ID;
                if (!bNotIsNull)
                {
                    this.ListCanBoLop.Add(oCanBoLop);
                }
            }
            else
            {
                oCanBoLop = oLoad;
            }
        }
        #region CachedList CanBoLop
        private static List<CanBoLopEntity> _ListCanBoLop = new List<CanBoLopEntity>();
        public List<CanBoLopEntity> ListCanBoLop
        {
            get
            {
                return _ListCanBoLop;
            }
        }
        public static List<CanBoLopEntity> ListCanBoLopStatic
        {
            get
            {
                return _ListCanBoLop;
            }
        }
        public CanBoLopEntity FindCanBoLop(Int32 CanBoLop_ID)
        {
            CanBoLopEntity oResult = this.ListCanBoLop.Find(delegate(CanBoLopEntity oFind)
            {
                bool result = true;
                result &= oFind.CanBoLop_ID.Equals(CanBoLop_ID);
                return result;
            });
            return oResult;
        }
        public static void LoadAllCanBoLop(bool clearCache)
        {
            if (_ListCanBoLop.Count == 0)
                clearCache = true;
            if (!clearCache) return;
            CanBoLopServiceClient dao = new CanBoLopServiceClient();
            _ListCanBoLop = new List<CanBoLopEntity>(dao.SelectAll());
            if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
        }
        public CanBoLopEntity LoadCanBoLop(Int32 CanBoLop_ID)
        {
            CanBoLopEntity o = dao.SelectOne(CanBoLop_ID);
            return o;
        }
        #endregion
    }
}
